package com.example.springboot.repository;

import com.example.springboot.bean.Register;
import com.example.springboot.bean.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LoginRepository {

    @Select("select * from sb_user")
    @Results(value = {
            @Result(id=true,column = "id",property = "id"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "mail",property = "mail"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "status",property = "status"),
            @Result(column = "code",property = "code"),
    },id="loginResult")
    List<Register> findAll();

    @Select("select * from sb_user where phone = #{phone} and status=0")
    @ResultMap("loginResult")
    List<Register> selectPhone(String phone);

    @Select("select * from sb_user where username = #{username} and status=0")
    @ResultMap("loginResult")
    List<Register> selectName(String username);

    @Insert("insert into sb_user(phone, email, username, password,status,code)"
            +"values(#{phone}, #{email}, #{username}, #{password}, #{status}, #{code})")//这样改，默认是0
    int insertRegister(Register register);

    @Select("select * from sb_user where username = #{username}")
    @ResultMap("loginResult")
    Register selectRegister(String username);

//    /**
//     * 登录，根据用户状态为“1”来查询
//     * @param user
//     * @return
//     */
//    @Select("select * from sb_user where username=#{username} and password=#{password} and status=1")
//    @ResultMap("loginResult")
//    User loginUser(User user);

    /**
     * 点击邮箱中的激活码进行激活，根据激活码查询用户，之后再进行修改用户状态为1进行激活
     * @param code
     * @return
     */
    @Select("select * from sb_user where code = #{code}")
    @ResultMap("loginResult")
    public Register checkCode(String code);

    /**
     * 激活账户，修改用户状态为“1”进行激活
     * @param register
     */
    @Update("update sb_user set phone=#{phone},email=#{email},username=#{username},password=#{password},status=#{status},code=#{code}"
            +"where id=#{id}")
    public void updateUserStatus(Register register);


}
